<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>语音转文本</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }

        .container {
            max-width: 600px;
            margin: 0 auto;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .btn {
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            border: none;
            cursor: pointer;
        }

        .btn:hover {
            background-color: #0056b3;
        }

        .result {
            margin-top: 20px;
            padding: 10px;
            background-color: #f9f9f9;
            border: 1px solid #ddd;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>语音转文本</h1>
    <form id="uploadForm" enctype="multipart/form-data">
        <div class="form-group">
            <label for="audioFile">选择音频文件:</label>
            <input type="file" id="audioFile" name="audioFile" accept="audio/*" required>
        </div>
        <div class="form-group">
            <button type="submit" class="btn">上传并转换</button>
        </div>
    </form>
    <div id="result" class="result"></div>
</div>

<script>
    document.getElementById('uploadForm').addEventListener('submit', function (event) {
        event.preventDefault();

        const formData = new FormData();
        const fileInput = document.getElementById('audioFile');
        formData.append('audioFile', fileInput.files[0]);

        fetch('/speech-to-text', {
            method: 'POST',
            body: formData
        })
            .then(response => response.text())
            .then(data => {
                document.getElementById('result').innerText = data;
            })
            .catch(error => {
                console.error('Error:', error);
                document.getElementById('result').innerText = '发生错误，请重试。';
            });
    });
</script>
</body>
</html>
